﻿using System;
using System.IO;
using NUnit.Framework;

namespace Calais.UnitTests
{
    [TestFixture]
    public class Constructor
    {
        private string _apiKey;
        private string _baseUrl;
        private string _contentText;
        private string _contentHtml;
        private string _contentXml;
        private string _externalId;
        private string _submitterId;

        [SetUp]
        public void Setup()
        {
            _apiKey = File.ReadAllText(Environment.GetEnvironmentVariable("USERPROFILE") + @"\Desktop\CalaisApiKey.txt");
            _baseUrl = "http://calaisdotnet.opencalais.com/tag";
            _contentText = "I went on holiday and went to Berlin in Germany!";
            _contentHtml = "<html><head><title>Holiday Trip</title></head><body><h1>My Holiday</h1><p>I went on holiday and went to Berlin in Germany!</p></body></html>";
            _contentXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><title>My Holiday Trip</title><Body>I went on holiday and went to Berlin in Germany!</Body><Date>" + DateTime.Now + "</Date>";
            _externalId = "CDN01";
            _submitterId = "CalaisDotNetUnitTest";


            Assert.IsNotNull(_apiKey);
            Assert.AreEqual(24, _apiKey.Length);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Constructor_Empty_Content_Throw()
        {
            new CalaisDotNet(_apiKey, string.Empty);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Constructor_Check_Key_Length_Throw()
        {
            new CalaisDotNet("garbage", _contentHtml);
        }

        [Test]
        public void Constructor_Check_Key_Length_Pass()
        {
            var calais = new CalaisDotNet(_apiKey, _contentHtml);
            Assert.IsNotNull(calais);
        }

        [Test]
        public void Constructor_TwoArg_DetectContentType_Html()
        {
            var calais = new CalaisDotNet(_apiKey, _contentHtml);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Html, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(string.Empty, calais.BaseUrl);
        }

        [Test]
        public void Constructor_TwoArg_DetectContentType_Text()
        {
            var calais = new CalaisDotNet(_apiKey, _contentText);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.RawText, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(string.Empty, calais.BaseUrl);
        }

        [Test]
        public void Constructor_TwoArg_DetectContentType_Xml()
        {
            var calais = new CalaisDotNet(_apiKey, _contentXml);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Xml, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(string.Empty, calais.BaseUrl);
        }

        [Test]
        public void Constructor_ThreeArg_InputFormat_Html()
        {
            var calais = new CalaisDotNet(_apiKey, _contentXml, CalaisInputFormat.Html);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Html, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(string.Empty, calais.BaseUrl); 
        }

        [Test]
        public void Constructor_ThreeArg_InputFormat_Text()
        {
            var calais = new CalaisDotNet(_apiKey, _contentXml, CalaisInputFormat.Text);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Text, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(string.Empty, calais.BaseUrl);  
        }

        [Test]
        public void Constructor_ThreeArg_InputFormat_Xml()
        {
            var calais = new CalaisDotNet(_apiKey, _contentXml, CalaisInputFormat.Xml);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Xml, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(string.Empty, calais.BaseUrl);   
        }

        [Test]
        public void Constructor_FourArg_BaseUrl()
        {
            var calais = new CalaisDotNet(_apiKey, _contentXml, CalaisInputFormat.Xml, _baseUrl);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Xml, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(true, calais.AllowDistribution);
            Assert.AreEqual(true, calais.AllowSearch);
            Assert.AreEqual(string.Empty, calais.ExternalId);
            Assert.AreEqual(string.Empty, calais.Submitter);
            Assert.AreEqual(_baseUrl, calais.BaseUrl);
        }

        [Test]
        public void Constructor_Full_AllArgs()
        {
            var calais = new CalaisDotNet(_apiKey, _contentText, CalaisInputFormat.Html, _baseUrl, false, false, _externalId, _submitterId, true, true, true, string.Empty);

            Assert.IsNotNull(calais);
            Assert.AreEqual(CalaisInputFormat.Html, calais.InputFormat);
            Assert.AreEqual(CalaisOutputFormat.Simple, calais.OutputFormat);
            Assert.AreEqual(false, calais.AllowDistribution);
            Assert.AreEqual(false, calais.AllowSearch);
            Assert.AreEqual(_externalId, calais.ExternalId);
            Assert.AreEqual(_submitterId, calais.Submitter);
            Assert.AreEqual(_baseUrl, calais.BaseUrl);
            Assert.AreEqual("GenericRelations", calais.EnableMetadataType);
            Assert.AreEqual(true, calais.CalculateRelevanceScore);
            Assert.AreEqual(true,calais.DiscardMetadata);
            Assert.AreEqual("er/Company;er/Geo",calais.DiscardMetadataString);
        }
    }
}
